<template>
  <div class="createPost-container">

    <sticky :z-index="10" :class-name="'sub-navbar '">
      <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="addNewField">
        添加
      </el-button>
      <el-button v-loading="loading" type="warning" @click="backtoPreviousTags(false)">
        返回
      </el-button>
    </sticky>

    <el-dialog :visible.sync="dialogTableVisible" title="添加新参数" >
      <el-form ref="postForm" :model="postForm" label-width="80px" style="width:400px;">
        <el-form-item label="参数名称">
          <el-input v-model="postForm.U_Fd"></el-input>
        </el-form-item>
        <el-form-item label="参数格式">
          <el-select v-model="postForm.U_FdType" default-first-option>
            <el-option label="字符串" value="A"></el-option>
            <el-option label="数字" value="B"></el-option>
            <el-option label="日期" value="D"></el-option>
            <el-option label="整型数字" value="N"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="必填与否">
          <el-select v-model="postForm.U_IsReq" default-first-option>
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数位置">
          <el-input v-model="postForm.U_VisOrder" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitNewField">提交</el-button>
          <el-button @click="dialogTableVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.LineId }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="属于">
        <template slot-scope="scope">
          <span>{{ scope.row.Code }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" align="center" label="内容">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.U_Fd" class="edit-input" size="small" />
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              取消
            </el-button>
          </template>
          <span v-else>{{ row.U_Fd }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="内容格式">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-select v-model="row.U_FdType" >
              <el-option label="字符串" value="A"></el-option>
              <el-option label="数字" value="B"></el-option>
              <el-option label="日期" value="D"></el-option>
              <el-option label="整型" value="N"></el-option>
            </el-select>
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              取消
            </el-button>
          </template>
          <span v-else>{{ row.U_FdTypeName }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="必填">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-select v-model="row.U_IsReq" >
              <el-option label="必填" value="1"></el-option>
              <el-option label="选填" value="0"></el-option>
            </el-select>
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              取消
            </el-button>
          </template>
          <span v-else>{{ row.U_IsReqName }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.U_VisOrder }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="400">
        <template slot-scope="{row}">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(row)"
          >
            Ok
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="row.edit=!row.edit"
          >
            Edit
          </el-button>
          <el-button
            v-if="!row.edit && list.map(v=>v.U_Fd).indexOf(row.U_Fd) != list.length -1 "
            type="warning"
            size="small"
            icon="el-icon-edit"
            @click="moveToNextLocation(row)"
          >
            向下移位
          </el-button>
          <el-button
            v-if="!row.edit && list.map(v=>v.U_Fd).indexOf(row.U_Fd) != 0 "
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="moveToPrevLocation(row)"
          >
            向上移位
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <div style="margin-top:50px;">

    </div>
    <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->

  </div>
</template>

<script>
import axios from 'axios'
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'
import { fetchEDCDetails, updateBOUDFs, updateBOUDFLocations } from '@/api/res'

const defaultForm = {
  status: 'draft',
  MEQG_Name: '',
  Code: '',
  Name: '',
  U_VisOrder: '',
  Canceled: '',
  Enable: true,
  id: undefined,
  isUsed: false
}

export default {
  name: 'DropdownDetail',
  components: { Sticky, Pagination },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      console.log('-------validateRequire-----------',rule,value)
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(new Error(rule.field + '为必传项'))
      } else {
        callback()
      }
    }

    return {
      postForm: Object.assign({}, defaultForm),
      id:undefined,
      code:undefined,
      loading: false,
      tempRoute: {},
      flist: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      dialogTableVisible:false,
      postForm:{

      }
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      const code = this.$route.query && this.$route.query.code
      console.log('created----',id,code,this.$route)
      this.id = id
      this.code = code
      this.fetchData(code)
    }

    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    moveToPrevLocation(row){
      var _curIdx = this.list.map(v=>v.U_Fd).indexOf(row.U_Fd)
      var _prevIdx = this.list.map(v=>v.U_Fd).indexOf(row.U_Fd) - 1

      //console.log('moveToPrevLocation-1----',[_curIdx,_prevIdx])

      var _prev = JSON.parse(JSON.stringify(this.list[_prevIdx]))
      var _cur = JSON.parse(JSON.stringify(this.list[_curIdx]))
      //console.log('moveToPrevLocation-0----',row.LineId,_cur,_prev)

      // this.list[_curIdx].Code = _prev.Code
      // this.list[_curIdx].U_Fd = _prev.U_Fd
      // this.list[_curIdx].U_FdType = _prev.U_FdType
      // this.list[_curIdx].U_FdTypeName = _prev.U_FdTypeName
      // this.list[_curIdx].U_IsReq = _prev.U_IsReq
      // this.list[_curIdx].U_Decim = _prev.U_Decim

      // this.list[_prevIdx].Code = _cur.Code
      // this.list[_prevIdx].U_Fd = _cur.U_Fd
      // this.list[_prevIdx].U_FdType = _cur.U_FdType
      // this.list[_prevIdx].U_FdTypeName = _cur.U_FdTypeName
      // this.list[_prevIdx].U_IsReq = _cur.U_IsReq
      // this.list[_prevIdx].U_Decim = _cur.U_Decim

      //console.log('moveToPrevLocation-1----',_curIdx,this.list[_curIdx].U_VisOrder,_prevIdx,this.list[_prevIdx].U_VisOrder)
      //console.log('moveToPrevLocation-2----',_curIdx,this.list[_prevIdx].U_VisOrder,_prevIdx,this.list[_curIdx].U_VisOrder)

      const _prevU_VisOrder = _prev.U_VisOrder
      const _curU_VisOrder = _cur.U_VisOrder
      this.list[_curIdx].U_VisOrder = _prevU_VisOrder
      this.list[_prevIdx].U_VisOrder = _curU_VisOrder
      this.list = this.list.sort((a,b)=>a.U_VisOrder.localeCompare(b.U_VisOrder))

      var config = {
        Code: this.list[_curIdx].Code,
        LineId1 : this.list[_prevIdx].LineId,
        VisOrder1: this.list[_prevIdx].U_VisOrder,
        LineId2 : this.list[_curIdx].LineId,
        VisOrder2: this.list[_curIdx].U_VisOrder
      }
      updateBOUDFLocations(config).then(res=>{
        console.log('moveToPrevLocation-2----',config,res)
        this.$message({ message: '位置更新成功', type: 'success' })
      })
    },
    moveToNextLocation(row){
      var _curIdx = this.list.map(v=>v.U_Fd).indexOf(row.U_Fd)
      var _nextIdx = this.list.map(v=>v.U_Fd).indexOf(row.U_Fd) + 1
      var _next = JSON.parse(JSON.stringify(this.list[_nextIdx]))
      var _cur = JSON.parse(JSON.stringify(this.list[_curIdx]))
      //console.log('moveToNextLocation-1----',[_curIdx,_nextIdx])

      // this.list[_curIdx].Code = _next.Code
      // this.list[_curIdx].U_Fd = _next.U_Fd
      // this.list[_curIdx].U_FdType = _next.U_FdType
      // this.list[_curIdx].U_FdTypeName = _next.U_FdTypeName
      // this.list[_curIdx].U_IsReq = _next.U_IsReq
      // this.list[_curIdx].U_Decim = _next.U_Decim

      // this.list[_nextIdx].Code = _cur.Code
      // this.list[_nextIdx].U_Fd = _cur.U_Fd
      // this.list[_nextIdx].U_FdType = _cur.U_FdType
      // this.list[_nextIdx].U_FdTypeName = _cur.U_FdTypeName
      // this.list[_nextIdx].U_IsReq = _cur.U_IsReq
      // this.list[_nextIdx].U_Decim = _cur.U_Decim
      //console.log('moveToNextLocation-1----',_curIdx,this.list[_curIdx].U_VisOrder,_nextIdx,this.list[_nextIdx].U_VisOrder)
      //console.log('moveToNextLocation-2----',_curIdx,this.list[_nextIdx].U_VisOrder,_nextIdx,this.list[_curIdx].U_VisOrder)


      const _nextU_VisOrder = _next.U_VisOrder
      const _curU_VisOrder = _cur.U_VisOrder
      this.list[_curIdx].U_VisOrder = _nextU_VisOrder
      this.list[_nextIdx].U_VisOrder = _curU_VisOrder
      this.list = this.list.sort((a,b)=>a.U_VisOrder.localeCompare(b.U_VisOrder))

      var config = {
        Code: this.list[_curIdx].Code,
        LineId1 : this.list[_curIdx].LineId,
        VisOrder1: this.list[_curIdx].U_VisOrder,
        LineId2 : this.list[_nextIdx].LineId,
        VisOrder2: this.list[_nextIdx].U_VisOrder
      }

      updateBOUDFLocations(config).then(res=>{
        console.log('moveToNextLocation-2----',config,res)
        this.$message({ message: '位置更新成功', type: 'success' })
      })

    },
    onSubmit(){
      console.log('Submit',JSON.stringify(this.postForm))
    },
    addNewField(){
      this.dialogTableVisible = true;
      const _list = this.flist.sort((a,b)=>b.U_VisOrder-a.U_VisOrder)
      console.log('_list_sort',_list)
      const _lastVisOrder = _list[_list.length-1].U_VisOrder
      var _nextVisOrder = _lastVisOrder.substr(0,1)+(_lastVisOrder.substr(1,_lastVisOrder.length-1)*1+1).toString().padStart(3,'0')
      this.postForm = {
        Code:this.flist[0].Code,
        LineId: this.flist.length+1,
        U_VisOrder: _nextVisOrder,
        U_Fd:'',
        U_FdType:'A',
        U_IsReq:'1'
      }
    },
    onSubmitNewField(){
      this.dialogTableVisible = false
      //this.postForm.DocEntry = this.id

      var row = this.postForm
      row.U_FdTypeName = this.postForm.U_FdType
      row.originalU_Fd = this.postForm.U_Fd
      row.originalU_FdType = this.postForm.U_FdType
      row.originalU_IsReq = this.postForm.U_IsReq
      row.U_Decim = this.postForm.U_FdType == 'B' ? 2 : 0
      row.edit = false

      switch(row.U_FdType){
        case "A": row.U_FdTypeName = '字符串';break;
        case "B": row.U_FdTypeName = '数字';break;
        case "D": row.U_FdTypeName = '日期';break;
        case "M": row.U_FdTypeName = '文本';break;
        case "N": row.U_FdTypeName = '整型';break;
        default: row.U_FdType;break;
      }

      row.U_IsReqName = this.postForm.U_IsReq == '1' ? '必填' : '选填'

      console.log('onSubmitNewField',this.postForm, row)


      updateBOUDFs(this.postForm).then(res=>{
        console.log('updateBOUDFs-----', this.postForm, res)
        var row = this.postForm
        row.U_FdTypeName = this.postForm.U_FdType
        row.originalU_Fd = this.postForm.U_Fd
        row.originalU_FdType = this.postForm.U_FdType
        row.originalU_IsReq = this.postForm.U_IsReq
        row.U_Decim = this.postForm.U_FdType == 'B' ? 2 : 0
        row.edit = false

        switch(row.U_FdType){
          case "A": row.U_FdTypeName = '字符串';break;
          case "B": row.U_FdTypeName = '数字';break;
          case "D": row.U_FdTypeName = '日期';break;
          case "M": row.U_FdTypeName = '文本';break;
          case "N": row.U_FdTypeName = '整型';break;
          default: row.U_FdType;break;
        }

        row.U_IsReqName = this.postForm.U_IsReq == '1' ? '必填' : '选填'

        this.list.push(row)

        this.$message({
          message: '数据更新成功',
          type: 'success'
        })

      })

    },
    onChangeisUsed(val){
      this.postForm.Canceled = val
      console.log('onChangeisUsed---------',this.postForm.Canceled)
    },
    cancelEdit(row) {
      row.U_Fd = row.originalU_Fd
      row.U_FdType = row.originalU_FdType
      row.U_IsReq = row.originalU_IsReq
      row.edit = false
      console.log('cancelEdit---',row.LineId,row,this.list.filter(v=>v.LineId == row.LineId))
      // this.$message({
      //   message: 'The U_Fd has been restored to the original value',
      //   type: 'warning'
      // })
    },
    confirmEdit(row) {
      row.originalU_Fd = row.U_Fd
      row.originalU_FdType = row.U_FdType
      row.originalU_IsReq = row.U_IsReq

      if(row.U_FdType == 'A'){
        row.U_FdTypeName = '字符串'
      }else if(row.U_FdType == 'B'){
        row.U_FdTypeName = '数字'
      }else if(row.U_FdType == 'D'){
        row.U_FdTypeName = '日期'
      }else if(row.U_FdType == 'N'){
        row.U_FdTypeName = '整型'
      }else{
        row.U_FdTypeName = row.U_FdType
      }

      if(row.U_IsReq == '1'){
        row.U_IsReqName = '必填'
      }else{
        row.U_IsReqName = '选填'
      }

      row.DocEntry = this.id
      console.log('confirmEdit---',row.LineId,row,this.list)

      updateBOUDFs(row).then(res=>{
        console.log('updateBOUDFs-----', row, res)
        row.edit = false
        this.$message({
          message: '数据更新成功',
          type: 'success'
        })

      })

    },

    backtoPreviousTags(reflush){
      const title = 'Edit EDC'
      const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` })
      this.$store.dispatch('tagsView/delView', route).then(({ visitedViews }) => {
        const latestView = visitedViews.slice(-1)[0]
        console.log('latestView--------------',latestView)
        if (latestView) {
          this.$router.push(latestView.fullPath)
          if(reflush && reflush == true){
            this.$router.go(0)
          }
        } else {
          if (view.name === 'Dashboard') {
            this.$router.replace({ path: '/redirect' + view.fullPath })
          } else {
            this.$router.push('/')
          }
        }
      })
    },
    getList(){
      const { page, limit } = this.listQuery
      this.list = this.flist
      //this.list = this.flist.filter((item, index) => index < limit * page && index >= limit * (page - 1))
    },
    fetchDataTest(id){
      if(id == undefined){
        this.id = id
      }
      const { page, limit } = this.listQuery
      const list = [{"row_number":"1","DocEntry":"7","Code":"shsc","Name":"熟化生产数据"},{"row_number":"2","DocEntry":"6","Code":"fqzl","Name":"分切质量数据"},{"row_number":"3","DocEntry":"5","Code":"fqsc","Name":"分切生产数据"},{"row_number":"4","DocEntry":"4","Code":"tbzl","Name":"涂布质量数据"},{"row_number":"5","DocEntry":"3","Code":"tbsc","Name":"涂布生产数据"},{"row_number":"6","DocEntry":"2","Code":"hjzl","Name":"混胶质量数据"},{"row_number":"7","DocEntry":"1","Code":"hjsc","Name":"混胶生产数据"}];
      const dlist = [{"row_number":"1","Code":"tbzl","LineId":"23","U_Fd":"判定","U_FdType":"A","U_Rlist":"HGList","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A023"},{"row_number":"2","Code":"tbzl","LineId":"22","U_Fd":"备注","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A022"},{"row_number":"3","Code":"tbzl","LineId":"21","U_Fd":"断裂伸长率TD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A021"},{"row_number":"4","Code":"tbzl","LineId":"20","U_Fd":"断裂伸长率MD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A020"},{"row_number":"5","Code":"tbzl","LineId":"19","U_Fd":"拉升强度TD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A019"},{"row_number":"6","Code":"tbzl","LineId":"18","U_Fd":"拉升强度MD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A018"},{"row_number":"7","Code":"tbzl","LineId":"17","U_Fd":"热收缩率TD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A017"},{"row_number":"8","Code":"tbzl","LineId":"16","U_Fd":"热收缩率MD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A016"},{"row_number":"9","Code":"tbzl","LineId":"15","U_Fd":"击穿电压","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A015"},{"row_number":"10","Code":"tbzl","LineId":"14","U_Fd":"解卷力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A014"},{"row_number":"11","Code":"tbzl","LineId":"13","U_Fd":"B","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A013"},{"row_number":"12","Code":"tbzl","LineId":"12","U_Fd":"A","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A012"},{"row_number":"13","Code":"tbzl","LineId":"11","U_Fd":"L","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A011"},{"row_number":"14","Code":"tbzl","LineId":"10","U_Fd":"GU","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A010"},{"row_number":"15","Code":"tbzl","LineId":"9","U_Fd":"雾度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A009"},{"row_number":"16","Code":"tbzl","LineId":"8","U_Fd":"透光率","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A008"},{"row_number":"17","Code":"tbzl","LineId":"7","U_Fd":"剪切强度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A007"},{"row_number":"18","Code":"tbzl","LineId":"6","U_Fd":"剥离强度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A006"},{"row_number":"19","Code":"tbzl","LineId":"5","U_Fd":"达因值","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A005"},{"row_number":"20","Code":"tbzl","LineId":"4","U_Fd":"接触电阻","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A004"},{"row_number":"21","Code":"tbzl","LineId":"3","U_Fd":"表面电阻","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A003"},{"row_number":"22","Code":"tbzl","LineId":"2","U_Fd":"粘性","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A002"},{"row_number":"23","Code":"tbzl","LineId":"1","U_Fd":"厚度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A001"},{"row_number":"24","Code":"tbsc","LineId":"22","U_Fd":"备注","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A022"},{"row_number":"25","Code":"tbsc","LineId":"21","U_Fd":"剥离收卷张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A021"},{"row_number":"26","Code":"tbsc","LineId":"20","U_Fd":"贴膜张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A020"},{"row_number":"27","Code":"tbsc","LineId":"19","U_Fd":"贴膜储料架张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A019"},{"row_number":"28","Code":"tbsc","LineId":"18","U_Fd":"收料张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A018"},{"row_number":"29","Code":"tbsc","LineId":"17","U_Fd":"收料摆动轮张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A017"},{"row_number":"30","Code":"tbsc","LineId":"16","U_Fd":"烘箱张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A016"},{"row_number":"31","Code":"tbsc","LineId":"15","U_Fd":"放料储料架张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A015"},{"row_number":"32","Code":"tbsc","LineId":"14","U_Fd":"放料张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A014"},{"row_number":"33","Code":"tbsc","LineId":"13","U_Fd":"烘箱7温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A013"},{"row_number":"34","Code":"tbsc","LineId":"12","U_Fd":"烘箱6温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A012"},{"row_number":"35","Code":"tbsc","LineId":"11","U_Fd":"烘箱5温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A011"},{"row_number":"36","Code":"tbsc","LineId":"10","U_Fd":"烘箱4温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A010"},{"row_number":"37","Code":"tbsc","LineId":"9","U_Fd":"烘箱3温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A009"},{"row_number":"38","Code":"tbsc","LineId":"8","U_Fd":"烘箱2温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A008"},{"row_number":"39","Code":"tbsc","LineId":"7","U_Fd":"烘箱1温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A007"},{"row_number":"40","Code":"tbsc","LineId":"6","U_Fd":"贴合压力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A006"},{"row_number":"41","Code":"tbsc","LineId":"5","U_Fd":"贴合温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A005"},{"row_number":"42","Code":"tbsc","LineId":"4","U_Fd":"机速","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"1","U_VisOrder":"A004"},{"row_number":"43","Code":"tbsc","LineId":"3","U_Fd":"原材料批号","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A003"},{"row_number":"44","Code":"tbsc","LineId":"2","U_Fd":"涂胶实际宽度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A002"},{"row_number":"45","Code":"tbsc","LineId":"1","U_Fd":"涂布头间隙","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A001"},{"row_number":"46","Code":"shsc","LineId":"3","U_Fd":"熟化结束时间","U_FdType":"D1","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A003"},{"row_number":"47","Code":"shsc","LineId":"2","U_Fd":"熟化开始时间","U_FdType":"D1","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A002"},{"row_number":"48","Code":"shsc","LineId":"1","U_Fd":"熟化温度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A001"},{"row_number":"49","Code":"hjzl","LineId":"4","U_Fd":"判定","U_FdType":"A","U_Rlist":"HGList","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A004"},{"row_number":"50","Code":"hjzl","LineId":"3","U_Fd":"备注","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A003"},{"row_number":"51","Code":"hjzl","LineId":"2","U_Fd":"粘度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A002"},{"row_number":"52","Code":"hjzl","LineId":"1","U_Fd":"固含量","U_FdType":"B1","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A001"},{"row_number":"53","Code":"hjsc","LineId":"21","U_Fd":"备注","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A021"},{"row_number":"54","Code":"hjsc","LineId":"20","U_Fd":"步骤4搅拌速度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A020"},{"row_number":"55","Code":"hjsc","LineId":"19","U_Fd":"步骤4搅拌时间","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A019"},{"row_number":"56","Code":"hjsc","LineId":"18","U_Fd":"步骤4物料数量","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A018"},{"row_number":"57","Code":"hjsc","LineId":"17","U_Fd":"步骤4物料名称","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A017"},{"row_number":"58","Code":"hjsc","LineId":"16","U_Fd":"步骤4搅拌机","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A016"},{"row_number":"59","Code":"hjsc","LineId":"15","U_Fd":"步骤3搅拌速度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A015"},{"row_number":"60","Code":"hjsc","LineId":"14","U_Fd":"步骤3搅拌时间","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A014"},{"row_number":"61","Code":"hjsc","LineId":"13","U_Fd":"步骤3物料数量","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A013"},{"row_number":"62","Code":"hjsc","LineId":"12","U_Fd":"步骤3物料名称","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A012"},{"row_number":"63","Code":"hjsc","LineId":"11","U_Fd":"步骤3搅拌机","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A011"},{"row_number":"64","Code":"hjsc","LineId":"10","U_Fd":"步骤2搅拌速度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A010"},{"row_number":"65","Code":"hjsc","LineId":"9","U_Fd":"步骤2搅拌时间","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A009"},{"row_number":"66","Code":"hjsc","LineId":"8","U_Fd":"步骤2物料数量","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A008"},{"row_number":"67","Code":"hjsc","LineId":"7","U_Fd":"步骤2物料名称","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A007"},{"row_number":"68","Code":"hjsc","LineId":"6","U_Fd":"步骤2搅拌机","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A006"},{"row_number":"69","Code":"hjsc","LineId":"5","U_Fd":"步骤1搅拌速度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A005"},{"row_number":"70","Code":"hjsc","LineId":"4","U_Fd":"步骤1搅拌时间","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A004"},{"row_number":"71","Code":"hjsc","LineId":"3","U_Fd":"步骤1物料数量","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A003"},{"row_number":"72","Code":"hjsc","LineId":"2","U_Fd":"步骤1物料名称","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A002"},{"row_number":"73","Code":"hjsc","LineId":"1","U_Fd":"步骤1搅拌机","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A001"},{"row_number":"74","Code":"fqzl","LineId":"23","U_Fd":"判定","U_FdType":"A","U_Rlist":"HGList","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A023"},{"row_number":"75","Code":"fqzl","LineId":"22","U_Fd":"备注","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A022"},{"row_number":"76","Code":"fqzl","LineId":"21","U_Fd":"断裂伸长率TD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A021"},{"row_number":"77","Code":"fqzl","LineId":"20","U_Fd":"断裂伸长率MD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A020"},{"row_number":"78","Code":"fqzl","LineId":"19","U_Fd":"拉升强度TD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A019"},{"row_number":"79","Code":"fqzl","LineId":"18","U_Fd":"拉升强度MD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A018"},{"row_number":"80","Code":"fqzl","LineId":"17","U_Fd":"热收缩率TD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A017"},{"row_number":"81","Code":"fqzl","LineId":"16","U_Fd":"热收缩率MD","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A016"},{"row_number":"82","Code":"fqzl","LineId":"15","U_Fd":"击穿电压","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A015"},{"row_number":"83","Code":"fqzl","LineId":"14","U_Fd":"解卷力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A014"},{"row_number":"84","Code":"fqzl","LineId":"13","U_Fd":"B","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"0","U_VisOrder":"A013"},{"row_number":"85","Code":"fqzl","LineId":"12","U_Fd":"A","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"0","U_VisOrder":"A012"},{"row_number":"86","Code":"fqzl","LineId":"11","U_Fd":"L","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"0","U_VisOrder":"A011"},{"row_number":"87","Code":"fqzl","LineId":"10","U_Fd":"GU","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"0","U_VisOrder":"A010"},{"row_number":"88","Code":"fqzl","LineId":"9","U_Fd":"雾度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A009"},{"row_number":"89","Code":"fqzl","LineId":"8","U_Fd":"透光率","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A008"},{"row_number":"90","Code":"fqzl","LineId":"7","U_Fd":"剪切强度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A007"},{"row_number":"91","Code":"fqzl","LineId":"6","U_Fd":"剥离强度","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A006"},{"row_number":"92","Code":"fqzl","LineId":"5","U_Fd":"达因值","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A005"},{"row_number":"93","Code":"fqzl","LineId":"4","U_Fd":"接触电阻","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A004"},{"row_number":"94","Code":"fqzl","LineId":"3","U_Fd":"表面电阻","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A003"},{"row_number":"95","Code":"fqzl","LineId":"2","U_Fd":"粘性","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"0","U_VisOrder":"A002"},{"row_number":"96","Code":"fqzl","LineId":"1","U_Fd":"厚度","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A001"},{"row_number":"97","Code":"fqsc","LineId":"7","U_Fd":"备注","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A007"},{"row_number":"98","Code":"fqsc","LineId":"6","U_Fd":"零料","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"0","U_VisOrder":"A006"},{"row_number":"99","Code":"fqsc","LineId":"5","U_Fd":"产出卷数","U_FdType":"N","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"1","U_VisOrder":"A005"},{"row_number":"100","Code":"fqsc","LineId":"4","U_Fd":"分切宽度","U_FdType":"A","U_Rlist":"","U_RTable":"","U_Decim":"0","U_IsReq":"0","U_VisOrder":"A004"},{"row_number":"101","Code":"fqsc","LineId":"3","U_Fd":"机速","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"1","U_IsReq":"1","U_VisOrder":"A003"},{"row_number":"102","Code":"fqsc","LineId":"2","U_Fd":"收卷张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A002"},{"row_number":"103","Code":"fqsc","LineId":"1","U_Fd":"放卷张力","U_FdType":"B","U_Rlist":"","U_RTable":"","U_Decim":"2","U_IsReq":"1","U_VisOrder":"A001"}];

      var item = list.filter(item => item.row_number == id )
      var _main = JSON.parse(JSON.stringify(item))[0];

      var _sublist = dlist.filter(item => item.Code == _main.Code).sort((a,b)=>a.LineId - b.LineId)
      console.log('fetchDataTest---------',_sublist)

      _sublist.forEach(item=>{
        if(item.U_FdType == 'A'){
          item.U_FdTypeName = '字符串'
        }else if(item.U_FdType == 'B'){
          item.U_FdTypeName = '数字'
        }else if(item.U_FdType == 'D'){
          item.U_FdTypeName = '日期'
        }else if(item.U_FdType == 'M'){
          item.U_FdTypeName = '文本'
        }else if(item.U_FdType == 'N'){
          item.U_FdTypeName = '整型'
        }else{
          item.U_FdTypeName = item.U_FdType
        }

        if(item.U_IsReq == '1'){
          item.U_IsReqName = '必填'
        }else{
          item.U_IsReqName = '选填'
        }
      })

      this.flist = _sublist.map(v => {
        this.$set(v, 'edit', false)
        v.originalU_Fd = v.U_Fd
        v.originalU_FdType = v.U_FdType
        v.originalU_IsReq = v.U_IsReq
        return v
      })

      //this.flist = _sublist;
      this.list = _sublist.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      this.total = _sublist.length
      this.listLoading = false

      // set tagsview title
      this.setTagsViewTitle()

      // set page title
      this.setPageTitle()
    },

    fetchData(code) {

      var query = this.listQuery
      query.code = code
      fetchEDCDetails(query).then(res=>{
        var {flist, curPageList} = res

        flist.forEach(item=>{
          if(item.U_FdType == 'A'){
            item.U_FdTypeName = '字符串'
          }else if(item.U_FdType == 'B'){
            item.U_FdTypeName = '数字'
          }else if(item.U_FdType == 'D'){
            item.U_FdTypeName = '日期'
          }else if(item.U_FdType == 'M'){
            item.U_FdTypeName = '文本'
          }else if(item.U_FdType == 'N'){
            item.U_FdTypeName = '整型'
          }else{
            item.U_FdTypeName = item.U_FdType
          }

          if(item.U_IsReq == '1'){
            item.U_IsReqName = '必填'
          }else{
            item.U_IsReqName = '选填'
          }
        })

        flist = flist.map(v => {
          this.$set(v, 'edit', false)
          v.originalU_Fd = v.U_Fd
          v.originalU_FdType = v.U_FdType
          v.originalU_IsReq = v.U_IsReq
          return v
        })

        this.flist = flist;
        this.list = flist
        this.total = flist.length
        this.listLoading = false

        this.setTagsViewTitle()

        this.setPageTitle()
      })

    },
    setTagsViewTitle() {
      const title = '参数编辑'
      const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.id}` })
      this.$store.dispatch('tagsView/updateVisitedView', route)
    },
    setPageTitle() {
      const title = '参数编辑'
      document.title = `${title} - ${this.id}`
    },
    submitForm() {
      console.log(this.postForm)
      if (this.postForm.title.length === 0) {
        this.$message({
          message: '请填写标题',
          type: 'warning'
        })
        return
      }

      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          var fmData = new FormData();
          fmData.append('title',encodeURIComponent(this.postForm.title))
          fmData.append('description',encodeURIComponent(this.postForm.description))
          fmData.append('startDate',encodeURIComponent(this.postForm.startDate))
          fmData.append('cost',this.postForm.cost)
          fmData.append('showBillUrl',this.onChangeImgUrl)

          if(this.pics.length>0 && this.pics[0].raw != null){
            fmData.append('file', this.pics[0].raw, this.pics[0].name)
          }


          axios({
            method: "post",
            headers: {"Content-Type": "multipart/form-data"},
            url: "http://localhost:8099/wapi/updateCourse?userCode=test",
            data: fmData,
          }).then(resData => {
              if (resData.status == 200) {
                console.log(resData);

                this.$notify({
                  title: '成功',
                  message: '发布课程成功',
                  type: 'success',
                  duration: 2000
                })
                this.postForm.status = 'published'
                this.loading = false
              }
          }).catch(err=> {
            console.log('error submit!!',err);
            return false
          });

        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    draftForm() {
      if (this.postForm.description.length === 0 || this.postForm.title.length === 0) {
        this.$message({
          message: '请填写必要的标题和内容',
          type: 'warning'
        })
        return
      }
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}

// .avatar{
//   width: 200px;
//   height: 200px;
//   border-radius: 50%;
// }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
